<?php

class ProductController extends Controller {

    public function actionView() {
        try {
            $slug = isset($_GET['id']) ? $_GET['id'] : '';
            if ($slug !== '') {
                $model = Product::model()->findByAttributes(array(
                    'slug' => $slug
                        ));
                $criteria = new CDbCriteria();
                $criteria->with = 'category';
                $criteria->join = 'JOIN categories c on c.id = t.category_id';
                $criteria->condition = 't.category_id=:category_id AND t.slug !=:slug';
                $criteria->params = array(':category_id' => $model['category_id'], ':slug' => $slug);
                $criteria->limit = 4;
                $product_other = Product::model()->findAll($criteria);
                $this->render('view', array(
                    'model' => $model,
                    'product_other' => $product_other
                ));
            }
        } catch (Exception $e) {
            Yii::log("Invalid request. Please do not repeat this request again.");
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }
    }

    public function actionIndex() {
        //try {
            $slug = isset($_GET['id']) ? $_GET['id'] : '';
            $criteria = new CDbCriteria();
            $criteria->select = 't.*';
            $criteria->with = 'category';
            $criteria->join = 'JOIN categories c on c.id = t.category_id';
            if ($slug !== '') {
                $cate = Category::model()->findByAttributes(array('slug' => $slug));
                $criteria->condition = 't.active=:pactive AND c.active=:cactive AND c.slug=:slug';
                $criteria->params = array(':slug' => $slug, ':pactive' => 1, ':cactive' => 1);
            } else {
                $cate = Category::model()->findByAttributes(array('show_first_product' => 1));
                $criteria->condition = 't.active=:pactive AND c.active=:cactive AND c.show_first_product=:show_first';
                $criteria->params = array(':show_first' => 1, ':pactive' => 1, ':cactive' => 1);
            }
            $parent_cate = '';
            if($cate_id = MyFunctionCustom::checkCatebySlug($slug)){
                    $parent_cate = MyFunctionCustom::getProductByCategoryParent($cate_id);
            }
                //$criteria->limit = 1;
                //$count = Product::model()->count($criteria);
            //$pages = new CPagination($count);
            //$pages->pageSize = 1;
            //$pages->applyLimit($criteria);
                
                $product = Product::model()->findAll($criteria);
                $page = false;
//                if ($count > $pages->pageSize) {
//                    $page = true;
//                }
                $this->render('index', array(
                    'product' => $product,
                    'cate' => $cate,
                    'parents' => $parent_cate
                    //'pages' => $pages,
                    //'page' => $page,
                ));
//        } catch (Exception $e) {
//            Yii::log("Invalid request. Please do not repeat this request again.");
//            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
//        }
    }

    public function loadModel($id) {
        try {
            $model = Product::model()->findByPk($id);
            if ($model === null)
                throw new CHttpException(404, 'The requested page does not exist.');
            return $model;
        } catch (Exception $e) {
            Yii::log("Invalid request. Please do not repeat this request again.");
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }
    }

}

?>
